package com.ruoyi.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseCustomer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBaseCustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户信息Controller
 *
 * @author ruoyi
 * @date 2023-02-08
 */
@Api(tags = "客户信息api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/customer" )
public class BaseCustomerController extends BaseController {

    private final IBaseCustomerService iBaseCustomerService;

    /**
     * 查询客户信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseCustomer baseCustomer)
    {
        startPage();
        LambdaQueryWrapper<BaseCustomer> lqw = new LambdaQueryWrapper<BaseCustomer>();
        if (StringUtils.isNotBlank(baseCustomer.getCustomerCode())){
            lqw.eq(BaseCustomer::getCustomerCode ,baseCustomer.getCustomerCode());
        }
        if (StringUtils.isNotBlank(baseCustomer.getCustomerName())){
            lqw.like(BaseCustomer::getCustomerName ,baseCustomer.getCustomerName());
        }
        if (StringUtils.isNotBlank(baseCustomer.getCustomerDesc())){
            lqw.eq(BaseCustomer::getCustomerDesc ,baseCustomer.getCustomerDesc());
        }
        List<BaseCustomer> list = iBaseCustomerService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 查询客户信息列表所有
     */
    @ApiOperation(value = "查询客户信息列表所有")
    @GetMapping("/getList")
    public List<BaseCustomer> getList(BaseCustomer baseCustomer)
    {
        LambdaQueryWrapper<BaseCustomer> lqw = new LambdaQueryWrapper<BaseCustomer>();
        if (StringUtils.isNotBlank(baseCustomer.getCustomerCode())){
            lqw.eq(BaseCustomer::getCustomerCode ,baseCustomer.getCustomerCode());
        }
        if (StringUtils.isNotBlank(baseCustomer.getCustomerName())){
            lqw.like(BaseCustomer::getCustomerName ,baseCustomer.getCustomerName());
        }
        if (StringUtils.isNotBlank(baseCustomer.getCustomerDesc())){
            lqw.eq(BaseCustomer::getCustomerDesc ,baseCustomer.getCustomerDesc());
        }
        List<BaseCustomer> list = iBaseCustomerService.list(lqw);
        return list;
    }

    /**
     * 导出客户信息列表
     */
    @Log(title = "客户信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BaseCustomer baseCustomer) {
        LambdaQueryWrapper<BaseCustomer> lqw = new LambdaQueryWrapper<BaseCustomer>(baseCustomer);
        List<BaseCustomer> list = iBaseCustomerService.list(lqw);
        ExcelUtil<BaseCustomer> util = new ExcelUtil<BaseCustomer>(BaseCustomer. class);
        return util.exportExcel(list, "customer" );
    }

    /**
     * 获取客户信息详细信息
     */
    @GetMapping(value = "/{customerId}" )
    public AjaxResult getInfo(@PathVariable("customerId" ) Long customerId) {
        return AjaxResult.success(iBaseCustomerService.getById(customerId));
    }

    /**
     * 新增客户信息
     */
    @Log(title = "客户信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseCustomer baseCustomer) {
        baseCustomer.setCreateBy(SecurityUtils.getUsername());
        baseCustomer.setCreateTime(new Date());
        return toAjax(iBaseCustomerService.save(baseCustomer) ? 1 : 0);
    }

    /**
     * 修改客户信息
     */
    @Log(title = "客户信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseCustomer baseCustomer) {
        baseCustomer.setUpdateBy(SecurityUtils.getUsername());
        baseCustomer.setUpdateTime(new Date());
        return toAjax(iBaseCustomerService.updateById(baseCustomer) ? 1 : 0);
    }

    /**
     * 删除客户信息
     */
    @Log(title = "客户信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerIds}" )
    public AjaxResult remove(@PathVariable Long[] customerIds) {
        return toAjax(iBaseCustomerService.removeByIds(Arrays.asList(customerIds)) ? 1 : 0);
    }
}
